package com.lx.mobileplayer.mvp.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import com.lx.mobileplayer.R
import com.lx.mobileplayer.utils.ResourcesUtil

/** 类说明：线性列表的adapter
 *  create by liuxiong at 2019/4/25 0025 21:58
 */
abstract class BaseListAdapter<T>(layoutId:Int,data:ArrayList<T>) :BaseAdater<T>(layoutId,data){

    val TYPE_LOADMORE:Int=1000

    enum class Status{
        NORMAL,LOADING,LOAD_COMPLETE
    }
    var status=Status.NORMAL

    var loadMoreItemViewHodler:BaseViewHodler?=null

    var mLoadMoreListener:View.OnClickListener?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHodler {
        if(viewType==TYPE_LOADMORE){
            var view= LayoutInflater.from(parent.context).inflate(R.layout.item_load_more,parent,false)
            loadMoreItemViewHodler=BaseViewHodler(view)
            return BaseViewHodler(view)
        }
        return super.onCreateViewHolder(parent, viewType)
    }

    override fun onBindViewHolder(holder: BaseViewHodler, position: Int) {
        if(holder.itemViewType==TYPE_LOADMORE){
            /** 设置loadmore条目的点击监听 */
            holder.itemView.setOnClickListener {view->

                if(status==Status.NORMAL){
                    mLoadMoreListener?.let {
                        mLoadMoreListener?.onClick(view)
                    }
                }
            }
            /** 设置loadmore条目的显示状态 */
            setLoadMoreStatus(status)
        }else{
            super.onBindViewHolder(holder, position)
        }
    }

    override fun getItemViewType(position: Int): Int {
        if(position==data.size){
            return TYPE_LOADMORE
        }

        return super.getItemViewType(position)
    }

    override fun getItemCount(): Int {
        return data.size+1
    }

    /**
    * 方法说明:  设置loadMore条目的状态
    * created by liuxiong on 2019/4/26 11:14
    */
    fun setLoadMoreStatus(currentStatus:Status){
        loadMoreItemViewHodler?.let {
            var pb=loadMoreItemViewHodler?.getView<ProgressBar>(R.id.progress_bar)
            var tvTip=loadMoreItemViewHodler?.getView<TextView>(R.id.tv_tip)

            when(currentStatus){
                Status.NORMAL-> {
                    pb?.visibility=View.GONE
                    tvTip?.setText(ResourcesUtil.getString(R.string.load_data_more))
                }
                Status.LOADING-> {
                    pb?.visibility=View.VISIBLE
                    tvTip?.setText(ResourcesUtil.getString(R.string.load_data_now))
                }
                Status.LOAD_COMPLETE-> {
                    pb?.visibility=View.GONE
                    tvTip?.setText(ResourcesUtil.getString(R.string.load_data_all_complete))
                }
                
            }
        }

    }

    /**
    * 方法说明:  设置loadmore条目的点击监听
    * created by liuxiong on 2019/4/26 11:18
    */
    fun setLoadMoreItemClickListener(listener:View.OnClickListener){
        this.mLoadMoreListener=listener
    }

}